package tag.system;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import share.storage.io.File;

public class Path extends TagSupport {

	File file = null;
	String type = "default";

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		if (file == null)
			return Path.SKIP_BODY;
		StringBuffer sb = new StringBuffer();
		if ("parent".equals(type))
			file = file.getParent();
		int count = 0;
		while (file != null && file.getParent() != null) {
			sb.insert(0, "/" + file.getFilename());
			file = file.getParent();
			count++;
		}
		if (count == 0)
			sb.insert(0, "/");
		try {
			pageContext.getOut().print(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	/**
	 * @param file
	 *            the file to set
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 5961748813650589670L;

}
